<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="85px" class="queryForms">
			<el-form-item label="零组件名称" prop="partName">
				<el-input maxlength="20" show-word-limit  v-model="queryParams.partName" placeholder="请输入零件名称" clearable @keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="零组件图号" prop="partFigure">
				<el-input maxlength="50" show-word-limit  v-model="queryParams.partFigure" placeholder="请输入零件图号" clearable @keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>
		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['erp:productPart:add']">新增</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['erp:productPart:remove']">删除</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button size="mini" type="info" @click="handleBacks">返回</el-button>
			</el-col>
			<right-toolbar @changeTableData="changeTableData" menuNames="productPart29" :showSearch.sync="showSearch" @queryTable="getList" :columns.sync="columns"></right-toolbar>
		</el-row>
		<div :class="sidebar.opened && eleTabTop <= 85 ? 'eleTab' : !sidebar.opened && eleTabTop <= 85 ? 'eleTabs' : ''" class="informationTab" ref="eleTab">
			<el-table ref="table" @row-click="row_click" v-loading="loading" :data="productPartList" @selection-change="handleSelectionChange" border height="100%">
				<el-table-column type="selection" width="55" align="center" />
				<el-table-column label="序号" align="center" width="55" type="index" />
				<el-table-column label="零组件名称" prop="partName" min-width="150" :show-overflow-tooltip="true" v-if="columns[0].visible" />
				<el-table-column label="零组件图号" prop="partFigure" min-width="150" :show-overflow-tooltip="true" v-if="columns[1].visible" />
				<el-table-column label="单套数量" prop="partQuantity" min-width="150" :show-overflow-tooltip="true" align="center" v-if="columns[2].visible" />
				<el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="320">
					<template slot-scope="scope">
						<el-button size="mini" type="text" icon="el-icon-edit" @click="handleSetting(scope.row)" v-hasPermi="['erp:productPart:settings']">设置套装库</el-button>
						<el-button size="mini" type="text" icon="el-icon-edit" @click="handleViews(scope.row)" v-hasPermi="['erp:productPart:views']">查看套装库</el-button>
						<el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['erp:productPart:edit']">修改</el-button>
						<el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['erp:productPart:remove']">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
		</div>
		<pagination menuNames="Pagination33" v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
		<!-- 添加或修改产品零组件对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="900px" append-to-body v-dialogDrag>
			<el-form ref="form" :model="form" :rules="rules" label-width="95px">
				<el-row :gutter="20">
					<el-col :span="12">
						<el-form-item label="零组件名称" prop="partName">
							<el-input v-model="form.partName" placeholder="请输入零组件名称" maxlength="20" show-word-limit  />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="零组件图号" prop="partFigure">
							<el-input v-model="form.partFigure" placeholder="请输入零组件图号" maxlength="50" show-word-limit  />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="单套数量" prop="partQuantity">
							<!-- <el-input-number v-model="form.partQuantity" :min="1" :max="100000000000" :step="1" step-strictly></el-input-number> -->
							<el-input v-model="form.partQuantity" maxlength="10" show-word-limit></el-input>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>

		<el-dialog title="设置套装库" :visible.sync="openSon" width="1600px" append-to-body v-dialogDrag>
			<div class="userDialog">
				<div class="userLeft">
					<el-form :model="queryParams1" ref="queryForm1" size="small" :inline="true" label-width="85px" class="queryForms">
						<el-form-item label="零组件图号" prop="partFigure">
							<el-input maxlength="50" show-word-limit  v-model="queryParams1.partFigure" placeholder="请输入零组件图号" clearable @keyup.enter.native="handleQuery1" />
						</el-form-item>
						<!-- <el-form-item label="零组件名称" prop="partName">
					<el-input maxlength="20" show-word-limit  v-model="queryParams1.partName" placeholder="请输入零组件名称" clearable @keyup.enter.native="handleQuery1" />
				</el-form-item> -->
						<el-form-item>
							<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery1">搜索</el-button>
							<el-button icon="el-icon-refresh" size="mini" @click="resetQuery1">重置</el-button>
						</el-form-item>
					</el-form>
					<el-table ref="sonTable" height="618px" :data="partList1" @selection-change="handleSelectionChangeSon" border row-key="id">
						<el-table-column type="selection" width="55" align="center" :reserve-selection="true" />
						<el-table-column label="序号" align="center" prop="id" type="index" width="55" />
						<el-table-column label="零组件图号" prop="partFigure" min-width="130" :show-overflow-tooltip="true" />
						<el-table-column label="零组件名称" prop="partName" min-width="100" :show-overflow-tooltip="true" />
						<el-table-column label="需求数量" prop="partQuantity" min-width="150" :show-overflow-tooltip="true" align="center">
							<template slot-scope="scope">
								<el-input-number :step="1" step-strictly :min="1" v-model="scope.row.partQuantity" style="width: 150px"></el-input-number>
							</template>
						</el-table-column>
						<el-table-column label="更换情况" prop="replacementSituation" min-width="160" :show-overflow-tooltip="true" align="center">
							<template slot-scope="scope">
								<el-input v-model="scope.row.replacementSituation" maxlength="200" placeholder="请输入更换情况"></el-input>
							</template>
						</el-table-column>
					</el-table>
					<pagination v-show="total1 > 0" :total="total1" :page.sync="queryParams1.pageNum" :limit.sync="queryParams1.pageSize" @pagination="getList1" />
				</div>

				<div class="userCenter">
					<div @click="setRight"><i class="el-icon-right"></i></div>
					<div @click="setLeft"><i class="el-icon-back"></i></div>
				</div>
				<div class="userRight">
					<el-table :data="selectList" height="618px" ref="rightTable" @selection-change="handleBackUser" border>
						<el-table-column type="selection" width="50" align="center" />
						<el-table-column label="序号" align="center" type="index" width="55" />
						<el-table-column label="零组件图号" prop="partFigure" min-width="100" :show-overflow-tooltip="true" />
						<el-table-column label="零组件名称" prop="partName" min-width="120" :show-overflow-tooltip="true" />
						<el-table-column label="数量" prop="partQuantity" min-width="80" align="center" />
						<el-table-column label="更换情况" prop="replacementSituation" min-width="180" :show-overflow-tooltip="true" />
					</el-table>
				</div>
			</div>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitFormSon">确 定</el-button>
				<el-button @click="closeSons">取 消</el-button>
			</div>
		</el-dialog>

		<el-dialog title="查看套装库" :visible.sync="viewSon" width="1000px" append-to-body v-dialogDrag>
			<el-table ref="sonTables" :data="partList2" border>
				<el-table-column label="序号" align="center" prop="id" type="index" width="55" />
				<el-table-column label="零组件图号" prop="partFigure" min-width="150" :show-overflow-tooltip="true" />
				<el-table-column label="零组件名称" prop="partName" min-width="150" :show-overflow-tooltip="true" />
				<el-table-column label="需求数量" prop="partQuantity" min-width="180" :show-overflow-tooltip="true" align="center"></el-table-column>
				<el-table-column label="更换情况" prop="replacementSituation" min-width="180" :show-overflow-tooltip="true" align="center"></el-table-column>
			</el-table>
			<pagination v-show="total2 > 0" :total="total2" :page.sync="queryParams2.pageNum" :limit.sync="queryParams2.pageSize" @pagination="getList2" />
		</el-dialog>
	</div>
</template>

<script>
import { listPart, batchAdd, partSuit, editpartSuit } from "@/api/part/part"
import { listProductPart, getProductPart, delProductPart, addProductPart, updateProductPart } from "@/api/product/productPart"
import { mapGetters } from "vuex"
import { getcolumn } from "@/api/selectList"
export default {
	name: "productPart",
	computed: {
		...mapGetters(["sidebar"])
	},
	data() {
		const blurText = async (rule, value, callback) => {
			const boolean = new RegExp("^[1-9][0-9]*$").test(value)
			if (!boolean) {
				if (value != "按需") {
					this.$message.warning("请输入正整数或者按需数据")
					this.form.partQuantity = null
				}
			}
		}
		return {
			paramsId:'',
			BackSelectionUser: [],
			selectList: [],
			queryParams2: {
				pageNum: 1,
				pageSize: 10
			},
			queryParams1: {
				pageNum: 1,
				pageSize: 10
			},
			sonRows: [],
			total2: 0,
			partList2: [],
			viewSon: false,
			sonId: "",
			total1: 0,
			partList1: [],
			Sonids: [],
			openSon: false,
			eleTabTop: 0, //表格容器距浏览器顶部距离
			// 遮罩层
			loading: true,
			// 选中数组
			ids: [],
			// 非单个禁用
			single: true,
			// 非多个禁用
			multiple: true,
			// 显示搜索条件
			showSearch: true,
			// 总条数
			total: 0,
			// 产品零组件表格数据
			productPartList: [],
			// 弹出层标题
			title: "",
			// 是否显示弹出层
			open: false,
			// 查询参数
			queryParams: {
				pageNum: 1,
				pageSize: 10,
				productId: null,
				partId: null,
				partName: null,
				partFigure: null,
				partQuantity: null,
				priceMaterial: null,
				manHour: null,
				fiftyManHour: null,
				partsPrice: null
			},
			// 表单参数
			form: {},
			// 表单校验
			rules: {
				partName: [{ required: true, message: "零组件名称不能为空", trigger: "blur" }],
				partFigure: [{ required: true, message: "零组件图号不能为空", trigger: "blur" }],
				partQuantity: [
					{ required: true, message: "单套数量不能为空", trigger: "blur" },
					{ validator: blurText, trigger: "blur" }
				]
			},
			// 列信息
			columns: [
				{ key: 0, label: `零组件名称`, visible: true },
				{ key: 1, label: `零组件图号`, visible: true },
				{ key: 2, label: `单套数量`, visible: true }
			]
		}
	},
	created() {
		// this.paramsId = this.$route.params.id 
		// this.$route.params.id && this.getSize()
	},
	mounted() {
		// 滚动条的获取
		this.handleScrollx()
		window.addEventListener("scroll", this.handleScrollx, true)
	},
	activated() {
		this.paramsId = this.$route.params.id 
		this.$route.params.id && this.getSize()
		//获取表格容器距页面头部的距离
		this.handleScrollx()
	},
	destroyed() {
		window.removeEventListener("scroll", this.handleScrollx, true)
	},
	methods: {
    getSize() {
			getcolumn({ pageName: "Pagination33" }).then(response => {
				this.queryParams.pageSize = response.data.length ? (response.data[0].limit ? response.data[0].limit : 10) : 10
				this.getList()
			})
		},
		// 单击表格行
		row_click(row, column, event) {
			if (column && column.label == "操作") {
				return
			} else {
				this.$refs.table.toggleRowSelection(row)
			}
		},
		changeTableData() {
			this.$nextTick(() => {
				this.$refs.table.doLayout()
			})
		},

		setRight() {
			let flag = this.sonRows.every(item => item.replacementSituation)
			if (flag) {
				this.selectList.push(...this.sonRows)
				this.selectList = this.removeSame1(this.selectList)
				this.$refs.sonTable.clearSelection()
			} else {
				this.$modal.msgSuccess("请先填写更换情况")
			}
		},
		// 去重
		removeSame1(arr) {
			let map = new Map()
			for (let item of arr) {
				// map.has()方法判断map中是否存在这个键
				// 不存在，则将键和值同时设置到map集合中，
				if (!map.has(item.id)) {
					map.set(item.id, item)
				}
			}
			// 返回map的值
			return [...map.values()]
		},
		handleBackUser(selection) {
			this.BackSelectionUser = selection
		},
		setLeft() {
			this.BackSelectionUser.forEach(item => {
				this.selectList.forEach((items, index) => {
					if (item.id == items.id) {
						this.selectList.splice(index, 1)
					}
				})
			})
		},
		editData(row) {
			editpartSuit(row).then(res => {
				this.$modal.msgSuccess("修改成功")
				this.getList2()
			})
		},
		handleViews(row) {
			this.sonId = row.id
			this.viewSon = true
			this.getList2()
		},
		getList2() {
			partSuit({
				parentId: this.sonId
			}).then(res => {
				this.partList2 = res.rows
				this.total2 = res.total
			})
		},
		submitFormSon() {
			let data = []
			this.selectList.forEach(item => {
				let params = {}
				params.partFigure = item.partFigure
				params.parentId = this.sonId
				;(params.partId = item.partId), (params.productPartId = item.id), (params.partName = item.partName)
				params.partQuantity = item.partQuantity
				params.replacementSituation = item.replacementSituation
				data.push(params)
			})
			batchAdd({
				parentId: this.sonId,
				partSuits: data
			}).then(res => {
				this.openSon = false
				this.$modal.msgSuccess("设置成功")
				this.$refs.sonTable.clearSelection()
			})
		},
		closeSons() {
			this.$refs.sonTable.clearSelection()
			this.openSon = false
		},
		handleSelectionChangeSon(selection) {
			this.Sonids = selection.map(item => item.id)
			this.sonRows = selection
		},
		handleSetting(row) {
			this.selectList = []
			this.sonId = row.id
			this.openSon = true
			this.getList1()
		},
		/** 重置按钮操作 */
		resetQuery1() {
			this.resetForm("queryForm1")
			this.handleQuery1()
		},
		handleQuery1() {
			this.queryParams1.pageNum = 1
			this.getList1()
		},
		/** 查询零组件库列表 */
		getList1() {
			this.queryParams1.productId = this.paramsId
			listProductPart(this.queryParams1).then(response => {
				this.partList1 = response.rows
				this.total1 = response.total
			})
		},
		handleBacks() {
			// 关闭当前tab页签，打开新页签
			const obj = { path: "/product/product_menu" }
			this.$tab.closeOpenPage(obj)
		},
		//获取表格容器距页面头部的距离
		handleScrollx() {
			this.eleTabTop = this.$refs.eleTab.getBoundingClientRect().top
		},
		/** 查询产品零组件列表 */
		getList() {
			this.loading = true
			this.queryParams.productId = this.paramsId
			listProductPart(this.queryParams).then(response => {
				this.productPartList = response.rows
				this.total = response.total
				this.loading = false
			})
		},
		// 取消按钮
		cancel() {
			this.open = false
			this.reset()
		},
		// 表单重置
		reset() {
			this.form = {
				id: null,
				productId: this.paramsId,
				partId: null,
				partName: null,
				partFigure: null,
				partQuantity: null,
				priceMaterial: null,
				manHour: null,
				fiftyManHour: null,
				partsPrice: null
			}
			this.resetForm("form")
		},
		/** 搜索按钮操作 */
		handleQuery() {
			this.queryParams.pageNum = 1
			this.getList()
		},
		/** 重置按钮操作 */
		resetQuery() {
			this.resetForm("queryForm")
			this.handleQuery()
		},
		// 多选框选中数据
		handleSelectionChange(selection) {
			this.ids = selection.map(item => item.id)
			this.single = selection.length !== 1
			this.multiple = !selection.length
		},
		/** 新增按钮操作 */
		handleAdd() {
			this.reset()
			this.open = true
			this.title = "添加产品零组件"
		},
		/** 修改按钮操作 */
		handleUpdate(row) {
			this.reset()
			const id = row.id || this.ids
			getProductPart(id).then(response => {
				this.form = response.data
				this.open = true
				this.title = "修改产品零组件"
			})
		},
		/** 提交按钮 */
		submitForm() {
			this.$refs["form"].validate(valid => {
				if (valid) {
					if (this.form.id != null) {
						updateProductPart(this.form).then(response => {
							this.$modal.msgSuccess("修改成功")
							this.open = false
							this.getList()
						})
					} else {
						addProductPart(this.form).then(response => {
							this.$modal.msgSuccess("新增成功")
							this.open = false
							this.getList()
						})
					}
				}
			})
		},
		/** 删除按钮操作 */
		handleDelete(row) {
			const ids = row.id || this.ids
			this.$modal
				.confirm("是否确认删除产品零组件数据项？")
				.then(function () {
					return delProductPart(ids)
				})
				.then(() => {
					this.getList()
					this.$modal.msgSuccess("删除成功")
				})
				.catch(() => {})
		},
		/** 导出按钮操作 */
		handleExport() {
			this.download(
				"erp/productPart/export",
				{
					...this.queryParams
				},
				`productPart_${new Date().getTime()}.xlsx`
			)
		}
	}
}
</script>
<style scoped lang="scss">
.userDialog {
	display: flex;
	justify-content: space-between;
	align-items: center;
	.userLeft {
		width: 52%;
	}
	.userRight {
		width: 45%;
	}
	.userRight {
		padding-bottom: 3px;
	}
	.userCenter {
		> div:first-child {
			margin-bottom: 50px;
		}
		.el-icon-right,
		.el-icon-back {
			font-size: 30px !important;
		}
	}
}
</style>
